﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gin.Controls;
using System.Threading;

namespace Gin.Commands
{
    delegate void AddControlDelegate(Control c);

    public class UserInput: Command
    {

        [GinArgumentUIControl(AllowMultiple = true)]
        public List<UserInputControl> InputControls { get; set; }

        [GinArgumentText(Multiline = false, MaxLength = 500)]
        public string FormCaption { get; set; }

        private AutoResetEvent _nextWaitHandle = new AutoResetEvent(false);
        private ExecutionContext _context;

        public override void Do(ExecutionContext context)
        {
            _context = context;

            context.ControlContainer.Invoke(new Action(context.ControlContainer.SuspendLayout), null);
            context.ControlContainer.Invoke(new Action(context.ControlContainer.Controls.Clear), null);

            int currentTop = 50;
            CreateUserInputControlsAt(ref currentTop);
            CreateCommonControlsAt(ref currentTop);

            context.ControlContainer.Invoke(new Action<bool>(context.ControlContainer.ResumeLayout), true);

            _nextWaitHandle.WaitOne();
        }

        private void CreateUserInputControlsAt(ref int currentTop)
        {
            foreach (UserInputControl input in InputControls)
            {
                Control control = input.Create(_context.ControlContainer.Width);
                
                control.Top = currentTop;
                control.Left = 0;
                control.Width = _context.ControlContainer.Width;
                control.Height = input.Height;
                control.Tag = input;
                currentTop += input.Height;

                //_context.ControlContainer.Controls.Add(control);
                _context.ControlContainer.Invoke(new AddControlDelegate(_context.ControlContainer.Controls.Add), control);

            }
        }

        private void CreateCommonControlsAt(ref int currentTop)
        {
            Button nextButton = new Button();
            nextButton.Top = currentTop;
            nextButton.Text = "Next";
            nextButton.Click += new EventHandler(nextButton_Click);
            currentTop += 20;

            _context.ControlContainer.Invoke(new AddControlDelegate(_context.ControlContainer.Controls.Add), nextButton);

        }

        void nextButton_Click(object sender, EventArgs e)
        {
            ReadValueFromUserInputs();
            _nextWaitHandle.Set();
        }

        private void ReadValueFromUserInputs()
        {
            foreach (Control control in _context.ControlContainer.Controls)
            {
                if (control.Tag is UserInputControl)
                {
                    SaveControlValueToContext(control);
                }
            }
        }

        private void SaveControlValueToContext(Control control)
        {
            UserInputControl input = (UserInputControl)(control.Tag);
            object value = input.ReadValue(control);
            _context.SaveUserInput(input.ResultName, value);
        }
    }
}
